package co.gov.medellin.ssti.sub.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

import javax.naming.NamingException;

import co.gov.medellin.ssti.sub.common.ConexionFactory;
import co.gov.medellin.ssti.sub.model.dao.DAOFactory;
import co.gov.medellin.ssti.sub.model.dao.IAreaDAO;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.vo.AreaVO;

public class AreaService {

	public Collection<AreaVO> findAreas(String strNombreArea)
			throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAreas(strNombreArea);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @return
	 * @throws DAOException
	 */
	public Collection<AreaVO> findAllAreas() throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAllAreas();
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strCode
	 * @return
	 * @throws DAOException
	 */
	public Collection<AreaVO> findArea(String strCode) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findArea(strCode);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param Arr
	 * @return
	 * @throws DAOException
	 */
	public int insertArea(String[] Arr) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.insertArea(Arr);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param Arr
	 * @param strCode
	 * @return
	 * @throws DAOException
	 */
	public int updateArea(String[] Arr, String strCode) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.updateArea(Arr, strCode);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param Arr
	 * @param strCode
	 * @return
	 * @throws DAOException
	 */
	public int updateSubArea(String[] Arr, String strCode) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.updateSubArea(Arr, strCode);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strCodeArea
	 * @return
	 * @throws DAOException
	 */
	public Collection<AreaVO> findAllSubAreas(String strCodeArea)
			throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAllSubAreas(strCodeArea);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strCodeUsu
	 * @param strGroupUser
	 * @return
	 * @throws DAOException
	 */
	public Collection<AreaVO> findAreasAuthorized(String strCodeUsu,
			String strGroupUser) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAreasAuthorized(strCodeUsu, strGroupUser);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strCodeUsu
	 * @param strGroupUser
	 * @return
	 * @throws DAOException
	 */
	public Collection<AreaVO> findAreasAuthorizedWithGestor(String strCodeUsu,
			String strGroupUser) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAreasAuthorizedWithGestor(strCodeUsu,
					strGroupUser);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strCodeProg
	 * @return
	 * @throws DAOException
	 */
	public AreaVO findAreaByProg(String strCodeProg) throws DAOException {
		IAreaDAO areaDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			areaDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getAreaDAO(
					con);
			return areaDAO.findAreaByProg(strCodeProg);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (areaDAO != null) {
				if (!areaDAO.isConnClosed()) {
					areaDAO.close();
				}
			}
		}
	}
}
